package com.orangelife.webservice.service.statistics.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.orangelife.service.statistics.IReportingService;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.utils.DateUtil;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.base.BaseService;
import com.orangelife.webservice.base.PageUtils;
import com.orangelife.webservice.service.statistics.IReportingWebService;
import com.orangelife.webservice.service.statistics.entity.StatisticsDto;

@Service("reportingWebService")
public class ReportingWebServiceImpl extends BaseService implements
		IReportingWebService {

	@Resource(name = "reportingService")
	private IReportingService reportingServicel;

	@Override
	public Response clearOwe(HttpServletRequest servletRequest, StatisticsDto st)
			throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validatePageAndSetPage(st.getPageIndex()
				+ "", st.getPageSize() + "");

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (StringUtils.isBlank(st.getCommID())) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
		}

		int y = DateUtil.getYear(st.getEndTime());
		st.setStartTime((y - 1) + "-12");
		List<Map<String, Object>> l = reportingServicel.clearOwe(st);
		Long total = 0l;
		if (st.getIsPage() == 1) {
			total = ((Page<Map<String, Object>>) l).getTotal();

		}

		return this.normalResp(st.getPageIndex(), st.getPageSize(), total,
				PropertyUtils.turnToEntity(l));
	}

	@Override
	public Response clearOweByComm(HttpServletRequest servletRequest,
			String commID, String endTime) throws Exception {
		if (StringUtils.isBlank(commID)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
		}
		if (StringUtils.isBlank(endTime)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "截止日期不能为空");
		}
		int y = DateUtil.getYear(endTime);
		String startTime = (y - 1) + "-12";

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("endTime", endTime);
		map.put("startTime", startTime);
		List<Map<String, Object>> l = PropertyUtils
				.turnToEntity(reportingServicel.clearOweByComm(map));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("clearOwe", l.get(0));
		return this.normalResp(result);
	}

	@Override
	public Response aging(HttpServletRequest servletRequest, String commID,
			String time, String feeTypeID) throws Exception {
		if (StringUtils.isBlank(commID)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
		}
		if (StringUtils.isBlank(time)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "时间不能为空");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("feeDate", time);
		map.put("feeTypeID", feeTypeID);
		List<Map<String, Object>> l = reportingServicel.agingByComm(map);

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("pageEntity", PropertyUtils.turnToEntity(l));
		result.put("errCode", 0);
		result.put("errInfo", "OK");
		return this.customResp(result);
	}

	@Override
	public Response payRateByComm(HttpServletRequest servletRequest,
			String feeTypeID, String commID, String time) throws Exception {
		if (StringUtils.isBlank(commID)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
		}
		if (StringUtils.isBlank(time)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "时间不能为空");
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("time", time);
		map.put("feeTypeID", feeTypeID);
		List<Map<String, Object>> l = PropertyUtils
				.turnToEntity(reportingServicel.payRateByComm(map));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("payRate", l.get(0));
		return this.normalResp(result);
	}

	@Override
	public Response payRateYearByComm(HttpServletRequest servletRequest,
			String commID, String time, String feeTypeID) throws Exception {
		if (StringUtils.isBlank(commID)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
		}
		if (StringUtils.isBlank(time)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "时间不能为空");
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("time", time);
		map.put("feeTypeID", feeTypeID);
		List<Map<String, Object>> l = PropertyUtils
				.turnToEntity(reportingServicel.payRateYearByComm(map));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("payRate", l.get(0));
		return this.normalResp(result);
	}

	@Override
	public Response fullIncome(HttpServletRequest servletRequest,
			StatisticsDto st) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validatePageAndSetPage(st.getPageIndex()
				+ "", st.getPageSize() + "");

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (StringUtils.isBlank(st.getCommID())) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
		}

		List<Map<String, Object>> l = reportingServicel.fullIncome(st);
		Long total = 0l;
		if (st.getIsPage() == 1) {
			total = ((Page<Map<String, Object>>) l).getTotal();

		}

		return this.normalResp(1, st.getPageSize(), total,
				PropertyUtils.turnToEntity(l));
	}

	@Override
	public Response fullIncomeByComm(HttpServletRequest servletRequest,
			String time, String commID) throws Exception {
		if (StringUtils.isBlank(commID)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
		}
		if (StringUtils.isBlank(time)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "统计月份不能为空");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("feeDate", time);
		List<Map<String, Object>> l = PropertyUtils
				.turnToEntity(reportingServicel.fullIncomeByComm(map));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("fullIncome", l.get(0));
		return this.normalResp(result);
	}

	@Override
	public Response getAllPropFeeAccountInfo(HttpServletRequest servletRequest,
			String commID, String time) throws Exception {
		if (StringUtils.isBlank(commID)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
		}
		if (StringUtils.isBlank(time)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "时间不能为空");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("time", time);
		List<Map<String, Object>> l = PropertyUtils
				.turnToEntity(reportingServicel.getAllPropFeeAccountInfo(map));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("accountInfo", l.get(0));
		return this.normalResp(result);
	}

	@Override
	public Response payRate(HttpServletRequest servletRequest, StatisticsDto st)
			throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validatePageAndSetPage(st.getPageIndex()
				+ "", st.getPageSize() + "");

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (StringUtils.isBlank(st.getCommID())) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
		}

		List<Map<String, Object>> l = reportingServicel.payRate(st);
		Long total = 0l;
		if (st.getIsPage() == 1) {
			total = ((Page<Map<String, Object>>) l).getTotal();

		}

		return this.normalResp(1, st.getPageSize(), total,
				PropertyUtils.turnToEntity(l));
	}

	@Override
	public Response payRateDetail(HttpServletRequest servletRequest,
			StatisticsDto st) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validatePageAndSetPage(st.getPageIndex()
				+ "", st.getPageSize() + "");

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (StringUtils.isBlank(st.getCommID())) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
		}

		List<Map<String, Object>> l = reportingServicel.payRateDetail(st);
		Long total = 0l;
		if (st.getIsPage() == 1) {
			total = ((Page<Map<String, Object>>) l).getTotal();

		}

		return this.normalResp(1, st.getPageSize(), total,
				PropertyUtils.turnToEntity(l));
	}

	@Override
	public Response getOwerOweFeeInfo(HttpServletRequest servletRequest,
			StatisticsDto st) throws Exception {

		if (StringUtils.isBlank(st.getCommID())) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
		}

		if (StringUtils.isBlank(st.getIsall())) {

			st.setIsall("1");

		}
		List<Map<String, Object>> l = reportingServicel.getOwerOweFeeInfo(st);
		Long total = 0l;
//		if (st.getIsPage() == 1) {
//			total = ((Page<Map<String, Object>>) l).getTotal();
//
//		}

		return this.normalResp(st.getPageIndex(), st.getPageSize(), total,
				PropertyUtils.turnToEntity(l));

	}

}
